<html xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8" />
<title>CS 143 Project 1 Part A</title>
</head>
<body lang=EN-US link=blue vlink=purple bgcolor="#FFFFFF">
	<h1 align="center"><b>Project 1 Part A</b></h1>
 
<h3><a href='./index.html'>Return to main index</a></h3>

<h3>Scope</h3>
	<p> The primary purpose of this first &quot;PHP warm-up&quot; part is
		for us to provide you with a whole bunch of basic information, and to get
		everyone up-to-speed on our computing systems and the languages and tools we
		will be using. Those of you who have done some Web
		programming before, especially in PHP,
		may find this project part nearly trivial. Those of you who haven't will find
		it merely straightforward. With all that said, <i>please don't start at the
		last minute</i> -- as with all programming and systems work, everything takes a
	bit of time, and unforeseen snafus do crop up. </p>
 
 
<p>The provided virtual machine image is based on Ubuntu 7.10, MySQL 5.0.45, Apache 2.2.4, and PHP 5.2.3.  You will need to use the provided VirtualBox guest OS to develop and test all projects for this class.  Your VirtualBox guest is essentially a Linux machine, which is a variant of Unix.</p>
 
<h3>PHP Web Calculator</h3>
<p>In this part of the project you will familiarize yourself with Apache2/PHP 
by building a small Web calculator application in PHP. 
 
</p><ul>
<li><b>Step 1:</b> Review the <a href="http://www.w3schools.com/php/php_intro.asp">W3CSchools PHP tutorial</a> to learn basic PHP. Please read at least up to "PHP $_POST" page of the tutorial. You can test the examples in the tutorial by creating a php page in the <code>${HOME}/www/</code> directory in the guest OS, which is aliased (or symbolic linked) to the VirtualBox shared directory. All files in <code>${HOME}/www/</code> are served by the guest Apache server and are accessible at <code>http://[guest IP]/~cs143/</code> from your host browser. Note that <code>${HOME}</code> is common Unix notation to refer to your home directory, which is <code>/home/cs143/</code> in our setup and <tt>[guest ip]</tt> is the IP of your guest machine, which is set up to be <tt>192.168.56.20</tt>.<p>
 
</p></li><li><b>Step 2:</b> Play with our demo calculator at <a href="http://oak.cs.ucla.edu/cs143/project/demo/calc/">http://oak.cs.ucla.edu/cs143/project/demo/calc/</a>
to understand what it does.<p>
 
</p></li><li><b>Step 3:</b> Implement your own calculator and make it available at
<code>http://[guest IP]/~cs143/calculator.php</code>. At the minimum, your calculator application should satisfy the following requirements.<p>
 
  </p><ol>
  <li>It should support +, -, * and / operators and the evaluation of the input should follow the standard operator precedence (i.e., the operators should be left-associative and + and * operators have precedence over + and -).
  </li><li>It should take both interger (like 1234) and real (like 123.45) numbers.
  </li><li>It <em>does not</em> need to support parentheses. (As a side note, in case you took the compiler class before, you may remember that the correct handling of nested-parentheses requires more expressive power than regular expression, like context-free grammar.)
  </li><li>It should handle any errors gracefully. For example, even if the user input is invalid expression, the result page should not display raw PHP error message. 
  </li><li>Your calculator should be implemented as a <em>single</em> .php page. Make sure that it does not include and/or read any other file (like CSS file, images or other HTML files).
  </li><li>For all links in your calculator, including form actions, you should use <em><b>relative URLs</b></em> instead of absolute. The reason is simple: depending on where we run your Web calculator, the hostname and the path of your calculator may change. So if you use absolute URL, you calculator may <em>break</em> when we try to grade it and we may have to give zero point for your work!
  </li>
  <li>
  The calculator.php is more like a question-answering interface, and therefore you should use <strong>HTTP GET</strong> protocol to process the user input as <a href="http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist">suggested by W3C</a>.
  </li>
  </ol><p>
In implementing your calculator, you may find the PHP functions, <code><a href="http://us2.php.net/preg_match" target="_top">preg_match()</a></code> and <code><a href="http://us2.php.net/eval" target="_top">eval()</a></code> helpful. If you are not familiar with regular expression, read a tutorial on regular expression like <a href="http://www.ibm.com/developerworks/library/os-php-regex1/index.html" target="_top">Mastering regular expression in PHP</a>.  If you are not familiar with the HTML input forms, you may also find our <a href="php/php_input.html">tutorial on PHP input handling</a> helpful. </p><p>
 
</p></li></ul>
Throughly test your code and make sure that it meets the above minimum requirements and runs correctly on our virtual machine. Note that this part of the project will be graded based on the functionality not on the look or style. As long as you meet the minimum requirements, you will get full credit for this project.
 
<h2>What to Submit</h2>
<p>Please submit the following files electronically:
</p><ul type=disc>
                <li>Your <code>calculator.php</code> source code. Again, please make sure that all URLs in your code are <em><b>relative</b></em>. You may get zero point otherwise.                </li>
                <li>A README file that includes any information you think is useful. At a minimum, your README must contain your name, SID, and email address. If you are working as a team, make sure both team member's information is included, and discuss briefly how the work was divided (For example, did you divide up the project and work separately? did you use pair programming? etc.). <strong>Your README file must be in PDF or PLAIN TEXT format. No Word documents, RTF, or any other format.</strong></li>
        		</ul>
<h2>FAQ</h2>
<ol>
<li><h4>Q: Does the calculator need to handle negative/positive numbers?</h4>
<p>
A: We will not use any positive sign before a number, but a negative sign needs to be supported. 
For example, we may test expressions like "3*-2" or "-2/-3" or "1+-1".
</li>
 
<li><h4>Q: how should we treat the fractional number: 
for example: 
would we treat ".123" as "0.123" or treat it as an invalid expression? </h4>
<p>
A: All fractional numbers will have a leading zero in our test, but you 
are welcome to handle .123 
</li>
 
<li>
  <h4>Q: may TAs give us some testing cases to help us to get a 100% score?</h4>
  <p>
      <ul>
        <li>-49 (Ans: -49)</li>
        <li>2+3+4 (Ans: 9)</li>
        <li>2*3*-4 (Ans: -24)</li>
        <li>2*-1*-2*-3 (Ans: -12)</li>
        <li>100-100/100 (Ans: 99)</li>
        <li>3/2+1/3 (Ans: close or equal to 1.83333333333)</li>
        <li>2- -1 (Ans: 3 or say it's an invalid expression)</li>
        <li>0/0 (NO excpetion shown on the page)</li>
        <li>abcd (Invalid Expression)</li>
        <li>one/two (Invalid Expression)</li>
      </ul>
  </p>
</li>
 
</ol>
</body>
</html>

